package demo02;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author wql
 * @version 1.0
 * @date 2022/11/9 17:21
 */

public class ProxyFactory {
    //多态的写法
    private Lease lease;

    public ProxyFactory(Lease lease) {
        this.lease = lease;
    }
    public Lease getInstance(){
        ClassLoader classLoader = lease.getClass().getClassLoader();
        Class<?>[] interfaces = lease.getClass().getInterfaces();
        InvocationHandler handler = new InvocationHandler(){

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                watch();
                Object invoke = method.invoke(lease, args);
                tuijian();
                return null;
            }
        };
        Object haha = Proxy.newProxyInstance(classLoader, interfaces, handler);
        return (Lease) haha;
    }
    public void watch(){
        System.out.println("看房");
    }
    public void tuijian(){
        System.out.println("房源推荐");
    }
}
